package com.wd.health.view.activity.mine;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.wd.health.R;
import com.wd.health.model.bean.UpdatePwdBean;
import com.wd.health.utils.RetrofitManager;
import com.wd.health.utils.RsaCoder;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class UpdatePwdActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView img_back;
    private EditText et_pwd;
    private EditText et_pwd2;
    private EditText et_pwd3;
    private Button btn_update;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_pwd);
        initView();
    }

    private void initView() {
        img_back = findViewById(R.id.img_back);
        et_pwd = findViewById(R.id.et_pwd);
        et_pwd2 = findViewById(R.id.et_pwd2);
        et_pwd3 = findViewById(R.id.et_pwd3);
        btn_update = findViewById(R.id.btn_update);
        img_back.setOnClickListener(this);
        btn_update.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.image_back:
                finish();
                break;
            case R.id.btn_update:
                String pwd1 = et_pwd.getText().toString();
                String pwd2 = et_pwd2.getText().toString();
                String pwd3 = et_pwd3.getText().toString();
                try {
                    String s1 = RsaCoder.encryptByPublicKey(pwd1);
                    String s2 = RsaCoder.encryptByPublicKey(pwd2);
                    String s3 = RsaCoder.encryptByPublicKey(pwd3);
                    RetrofitManager.getInstance().putUpdatePwdData(s1, s2, new Observer<UpdatePwdBean>() {
                        @Override
                        public void onSubscribe(Disposable d) {

                        }

                        @Override
                        public void onNext(UpdatePwdBean updatePwdBean) {
                            if (updatePwdBean.getMessage().equals("修改成功")){
                                Toast.makeText(UpdatePwdActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                                Intent intent = new Intent(UpdatePwdActivity.this, LoginActivity.class);
                                startActivity(intent);
                                finish();
                            }
                        }

                        @Override
                        public void onError(Throwable e) {

                        }

                        @Override
                        public void onComplete() {

                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;

        }
    }
}